<template>
  <div class="loginContainer">
    <div class="loginbox">
      <div class="avast">
        <img src="../assets/image/fupin.jpg" alt />
      </div>
      <div class="logininfo">
        <!-- 用户名密码框 -->
        <div class="userinfo">
          <el-input v-model="username" placeholder="请输入用户名"></el-input>
          <el-input
            v-model="password"
            placeholder="请输入密码"
            type="password"
            v-on:keyup.enter.native="getUserinfo"
          ></el-input>
        </div>
        <div class="login">
          <el-button type="primary" @click="getUserinfo">登录</el-button>
          <el-button type="info" @click="resetData">重置</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: 'admin',
      password: '123456'
    };
  },
  methods: {
    async getUserinfo(e) {
      // 获取用户数据的用户名和密码和固定的admin相比是否正确
      // if(this.username=='admin' && this.password=='123456'){
      //     alert('用户名密码正确')
      // }else {
      //     alert('用户名密码错误')
      // }
      // console.log(e);

      let { data: res } = await this.axios.get('/data/login.json');
      //   console.log(res);
      //   判断username和密码是否和获取到的数据相等
      var result = res.userinfo;
      var userresult = result.filter(item => {
        return (
          item.username === this.username &&
          item.password.trim() === this.password.trim()
        );
      });
      //   console.log(userresult, 77);
      if (userresult.length > 0) {
        // 存储token
        window.sessionStorage.setItem('token', userresult[0]['token']);
        this.$router.push('/home');
        // alert('用户名密码正确')
      } else {
        this.$message.error('用户名或密码错误');
      }
    },
    resetData() {
      (this.username = ''), (this.password = '');
    }
  }
};
</script>

<style lang="less" scoped>
.loginContainer {
  width: 100%;
  height: 100%;
  background-color: #0072bb;
  .loginbox {
    width: 450px;
    height: 300px;
    box-sizing: border-box;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    border-radius: 10px;
    transform: translate(-50%, -50%);
    .avast {
      width: 120px;
      height: 120px;
      position: absolute;
      padding: 10px;
      left: 50%;
      border-radius: 50%;
      border: 1px solid #eee;
      background-color: #fff;
      transform: translate(-50%, -50%);
      box-shadow: 0 0 10px #0072bb;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        border: 1px solid #eee;
      }
    }
    .logininfo {
      box-sizing: border-box;
      position: absolute;
      bottom: 40px;
      .userinfo {
        box-sizing: border-box;
        margin: 10px;
        .el-input {
          margin: 10px 0;
          width: 100%;
        }
      }
      .login {
        display: flex;
        justify-content: flex-end;
        .el-button {
          margin-right: 10px;
        }
      }
    }
  }
}
</style>